package org.cbioportal.legacy.web;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.Size;
import java.util.List;
import org.cbioportal.legacy.model.Geneset;
import org.cbioportal.legacy.service.GenesetService;
import org.cbioportal.legacy.service.exception.GenesetNotFoundException;
import org.cbioportal.legacy.web.config.annotation.InternalApi;
import org.cbioportal.legacy.web.parameter.HeaderKeyConstants;
import org.cbioportal.legacy.web.parameter.PagingConstants;
import org.cbioportal.legacy.web.parameter.Projection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@InternalApi
@RestController
@Validated
@Tag(name = "Gene Sets", description = " ")
public class GenesetController {

  @Autowired private GenesetService genesetService;

  @RequestMapping(
      value = "/api/genesets",
      method = RequestMethod.GET,
      produces = MediaType.APPLICATION_JSON_VALUE)
  @Operation(description = "Get all gene sets")
  @ApiResponse(
      responseCode = "200",
      description = "OK",
      content = @Content(array = @ArraySchema(schema = @Schema(implementation = Geneset.class))))
  public ResponseEntity<List<Geneset>> getAllGenesets(
      @Parameter(description = "Level of detail of the response")
          @RequestParam(defaultValue = "SUMMARY")
          Projection projection,
      @Parameter(description = "Page size of the result list")
          @Max(Integer.MAX_VALUE)
          @Min(PagingConstants.MIN_PAGE_SIZE)
          @RequestParam(defaultValue = PagingConstants.DEFAULT_PAGE_SIZE)
          Integer pageSize,
      @Parameter(description = "Page number of the result list")
          @Min(PagingConstants.MIN_PAGE_NUMBER)
          @RequestParam(defaultValue = PagingConstants.DEFAULT_PAGE_NUMBER)
          Integer pageNumber) {

    if (projection == Projection.META) {
      HttpHeaders responseHeaders = new HttpHeaders();
      responseHeaders.add(
          HeaderKeyConstants.TOTAL_COUNT,
          genesetService.getMetaGenesets().getTotalCount().toString());
      return new ResponseEntity<>(responseHeaders, HttpStatus.OK);
    } else {
      return new ResponseEntity<>(
          genesetService.getAllGenesets(projection.name(), pageSize, pageNumber), HttpStatus.OK);
    }
  }

  @RequestMapping(
      value = "/api/genesets/{genesetId}",
      method = RequestMethod.GET,
      produces = MediaType.APPLICATION_JSON_VALUE)
  @Operation(description = "Get a gene set")
  @ApiResponse(
      responseCode = "200",
      description = "OK",
      content = @Content(schema = @Schema(implementation = Geneset.class)))
  public ResponseEntity<Geneset> getGeneset(
      @Parameter(required = true, description = "Gene set ID e.g. GNF2_ZAP70") @PathVariable
          String genesetId)
      throws GenesetNotFoundException {

    return new ResponseEntity<>(genesetService.getGeneset(genesetId), HttpStatus.OK);
  }

  @RequestMapping(
      value = "/api/genesets/fetch",
      method = RequestMethod.POST,
      consumes = MediaType.APPLICATION_JSON_VALUE,
      produces = MediaType.APPLICATION_JSON_VALUE)
  @Operation(description = "Fetch gene sets by ID")
  @ApiResponse(
      responseCode = "200",
      description = "OK",
      content = @Content(array = @ArraySchema(schema = @Schema(implementation = Geneset.class))))
  public ResponseEntity<List<Geneset>> fetchGenesets(
      @Parameter(required = true, description = "List of Geneset IDs")
          @Size(min = 1, max = PagingConstants.MAX_PAGE_SIZE)
          @RequestBody
          List<String> genesetIds) {

    return new ResponseEntity<>(genesetService.fetchGenesets(genesetIds), HttpStatus.OK);
  }

  @RequestMapping(
      value = "/api/genesets/version",
      method = RequestMethod.GET,
      produces = MediaType.APPLICATION_JSON_VALUE)
  @Operation(description = "Get the geneset version")
  @ApiResponse(
      responseCode = "200",
      description = "OK",
      content = @Content(schema = @Schema(implementation = String.class)))
  public ResponseEntity<?> getGenesetVersion() {

    return new ResponseEntity<>("\"" + genesetService.getGenesetVersion() + "\"", HttpStatus.OK);
  }
}
